import java.util.Scanner;

public class Answer2 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        long n = in.nextLong();

        long price = in.nextLong();

        long ret = 0;
        long preMin = price;

        // 只要有利润就卖出 :当天卖当天又可以买
        for (int i = 2; i <= n; i++) {
            price = in.nextLong();
            if (price > preMin ) {
                ret += price - preMin;
                preMin = price;
            } else if (price <= preMin)
                preMin = price;
        }


        System.out.println(ret);
    }

/*    //买卖股票的最好时机(二)
    //https://www.nowcoder.com/practice/fbc5dad3e215457fb82a3ae688eb7281?tpId=230&tqId=39768&ru=/exam/oj
    // 贪心算法：每次只要有利润，就加
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] price = new int[n];
        for (int i = 0; i < n; i++) {
            price[i] = in.nextInt();
        }

        int ret = 0;
        for (int i = 1; i < n; i++) {
            int a = price[i] - price[i - 1];
            if (a > 0) {
                ret += a;
            }
        }
        System.out.println(ret);
    }*/

}
